This invention relates to software simulation of computer hardware and more specifically to a method for user code to distinguish whether it is executing on physical hardware or simulated hardware.
Architectural simulators are used primarily during the development of a new computer platform to simulate the physical hardware which is being designed. This allows software developers to begin creating programs for the new computer platform even before the hardware development has been completed. Architectural simulators allow software to be programmed and debugged so that it is ready to release with the completed computer platform.
The software, or user code, being programmed and tested is executed on the architectural simulator during hardware development. Once the physical hardware has been manufactured, the user code can be executed either on the physical hardware or the simulated hardware. The user code should preferably execute exactly the same way and produce the same results on either the simulated hardware or the physical hardware. It is therefore preferable to execute the same user code on both the simulated hardware and the physical hardware. However, since simulated hardware and physical hardware is not exactly the same, there are certain instances in which the user code needs to be execute differently depending upon whether it is executing in simulated hardware or physical hardware.
For example, simulated hardware almost always executes user code more slowly than physical hardware, since the simulated hardware is actually simulator code being executed on an existing computer system. The existing computer system is thus required to read the user code instructions, execute the simulator code to translate the user code instructions into instructions which are native to the existing computer system, then execute the native instructions. In contrast, physical hardware executing the same user code is only required to read the user code instructions and execute them. Therefore, the execution speed is almost never the same on the simulated hardware and the physical hardware. This becomes a problem when critical timing paths in the user code rely on execution speed, such as when a software timing loop operates by counting instructions. In this case, the user code must know how many instructions per second are executed by the simulated or physical hardware.
This type of problem is addressed in typical user code by including two alternate sections of code, one to be executed on simulated hardware and one to be executed on physical hardware, with conditional compilation flags in the code which include only one of the alternate sections in the compiled software. For example, if the user code is written in a high level language, such as C, it must be compiled into instructions which are part of the instruction set for the platform being designed. Compilation flags can be used to instruct the compiler to include or exclude portions of the user code in the compiled executable program. This allows the user code to be maintained in one location, regardless of whether it will be executed on simulated or physical hardware. The main portions of the user code which are hardware independent can be executed on either type, and hardware dependent sections are compiled according to the conditional compilation flags.
Unfortunately, this approach does have drawbacks. The programmer must modify the user code to indicate which of the alternate sections to include in the compiled executable program. In addition, two separate executable programs are created, one for simulated hardware and one for physical hardware. This doubles the storage space required for the user code and introduces the risk of using the wrong user code for the target hardware type.
To assist in achieving the aforementioned needs, the inventors have devised a method which allows user code to distinguish whether it is executing on physical hardware or simulated hardware. A single version of the user code may therefore be maintained which detects whether it is executing on simulated hardware or physical hardware and function accordingly, possibly with different code paths. In a preferred embodiment, simulated computer hardware is differentiated from physical computer hardware by the hardware simulator detecting a sequence of instructions issued by the user code which produce no effect on physical computer hardware and setting a flag. User code may thus issue the sequence of instructions, then check the flag to determine whether it is executing on simulated hardware or physical hardware. To minimize the likelihood that the sequence of instructions is accidentally produced by the user code, the sequence is preferably executed with no intervening instructions, including those in traps or interrupt handlers. The sequence is also preferably executed by a single processor in the simulator and contains exact predetermined parameter values in the proper order.
Thus, the invention may comprise a method of determining whether an electronic device is simulated. The method includes storing a first value in a memory location, then executing at least one operation on the electronic device. The at least one operation causes the memory location to change value only if the computer platform is simulated. The method also includes reading a second value from the memory location to determine whether the second value is different from the first value.
The invention may also comprise a method of indicating that an electronic device is a simulation, including comparing instructions for the electronic device with a predetermined sequence of instructions, and indicating that the electronic device is a simulation if the instructions include the predetermined sequence of instructions.
The invention may also comprise a computer hardware simulator having at least one computer readable storage medium containing computer readable program code. The computer readable program code includes code for receiving instructions for the computer hardware, code for comparing the instructions with a predetermined set of instructions, and code for setting a flag if the instructions contain the predetermined set of instructions. The flag indicates that the computer hardware is simulated rather than physical.
The invention may also comprise a computer hardware simulator having means for receiving an instruction sequence, means for comparing the instruction sequence with a predetermined instruction sequence, and means for indicating that the computer hardware is a simulation if the instruction sequence matches the predetermined instruction sequence.